package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.pojo.pro.ProductionTask;
import com.qidianit.mes.pro.mapper.ProductionTaskMapper;
import com.qidianit.mes.pro.service.ProductionTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Package Path: com.qidianit.mes.pro.service.impl
 * Author: Alhz
 * Description:
 * Date: 2025/9/24 14:27
 */
@Service
public class ProductionTaskServiceImpl implements ProductionTaskService {

    @Autowired
    private ProductionTaskMapper productionTaskMapper;

    @Override
    public Result selectAllProductionTask(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ProductionTask> productionTaskList = productionTaskMapper.selectAllProductionTask();
        PageInfo<ProductionTask> pageInfo = new PageInfo<>(productionTaskList);
        return Result.success(pageInfo);
    }

    @Override
    public Result selectAllProductionOrders(Integer productionOrderId,  Integer processId,int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ProductionTask> productionTasks = productionTaskMapper.selectAllProductionOrders(productionOrderId,processId);
        PageInfo<ProductionTask> pageInfo = new PageInfo<>(productionTasks);

        return Result.success(pageInfo);
    }

    @Override
    public Result getProductionTaskById(Long id) {
        return Result.success(productionTaskMapper.getProductionTaskById(id));
    }

    @Override
    public Result addProductionTask(ProductionTask productionTask) {
        productionTask.setCreateTime(LocalDateTime.now());
        productionTask.setUpdateTime(LocalDateTime.now());
        productionTask.setDeleteFlag(0);
        return Result.success(productionTaskMapper.addProductionTask(productionTask));
    }

    @Override
    public Result updateProductionTask(ProductionTask productionTask) {
        return Result.success(productionTaskMapper.updateProductionTask(productionTask));
    }

    @Override
    public Result deleteProductionTask(Long id) {
        return Result.success(productionTaskMapper.deleteProductionTask(id));
    }

    @Override
    public Result batchDeleteProductionTasks(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("删除的任务ID列表不能为空");
        }

        int deletedCount = productionTaskMapper.batchDeleteProductionTasks(ids);
        if (deletedCount > 0) {
            return Result.success("成功删除 " + deletedCount + " 个生产任务");
        } else {
            return Result.error("批量删除失败");
        }
    }
}